package com.wf.controller;

import com.wf.provider.MenuProvider;
import com.wf.provider.RoleProvider;
import com.wf.provider.UserProvider;
import com.wf.security.MyUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by yuzhongbo on 2017/5/3.
 */
@Controller
public class LoginController {

    @Autowired
    UserProvider userProvider;
    @Autowired
    RoleProvider roleProvider;
    @Autowired
    MenuProvider menuProvider;

    @RequestMapping("/")
    public ModelAndView index(Model model){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("home");
        return mv;
    }

    @RequestMapping("/login")
    public  String login(){
        return "login";
    }

    private MyUserDetails getLoginUser(){
        return (MyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }
    /***
     * 显示权限树
     */
    @RequestMapping("/leftTree")
    @ResponseBody
    public String getLeftTree(){
        MyUserDetails user = getLoginUser();
        return menuProvider.getMenusJsonByUserId(user.getId());
    }
    @RequestMapping("/security")
    public String security(HttpSession session) {
      return "home";
    }


}